﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Movies
{
    public class HeplerBGProcesses
    {
        private static List<HeplerBGProcesses> list = new List<HeplerBGProcesses>();

        private HeplerBGProcesses()
        {
        }

        private static Form bgProcessForm;

        public static void showProgressDetails()
        {
            try
            {
                bgProcessForm.Hide();
                bgProcessForm.Show();
            }
            catch (Exception ex) { HelperContentProvider.OmitWarning(ex); }
        }

        public static void addBGProcess(Form formInfo, ProgressBar progressInfo, String labelInfo, bool showOnDone){
            HeplerBGProcesses bgProcess = new HeplerBGProcesses();
            bgProcess.Form = formInfo;
            bgProcess.ProgressBar = progressInfo;
            bgProcess.Label = labelInfo;
            bgProcess.ShowOnDone = showOnDone;
            list.Add(bgProcess);
            reloadBGProcessWin();
        }

        private static void removeBGProcess(HeplerBGProcesses helper)
        {
            list.Remove(helper);
        }

        public static void reloadBGProcessWin()
        {
            List<HeplerBGProcesses> listNew = new List<HeplerBGProcesses>();

            if (bgProcessForm != null && !bgProcessForm.IsDisposed)
            {
                bgProcessForm.Dispose();
            }
            foreach(HeplerBGProcesses helper in list){
                if (!helper.Completed)
                {
                    listNew.Add(helper);
                }
            }
            list = listNew;
            bgProcessForm = new FormBGProcesses(list);
        }



        private String label;
        public String Label
        {
            get
            {
                return label;
            }
            set
            {
                label = value;
            }
        }

        private Form form;
        public Form Form
        {
            get
            {
                return form;
            }
            set
            {
                form = value;
            }
        }

        private ProgressBar progressBar;
        private ProgressBar newProgressBar;

        private delegate int UpdateProgress();
        public int updateProgress()
        {
            if (newProgressBar.InvokeRequired)
            {
                object intRet = 100;
                try
                {
                    UpdateProgress up = new UpdateProgress(updateProgress);
                    intRet = WinDisplayControllor.MDIParent.Invoke(up, new object[] { });
                }
                catch (Exception ex)
                { HelperContentProvider.OmitWarning(ex); }

                int val = Convert.ToInt32(intRet);
                return val;
            }
            else
            {
                newProgressBar.Maximum = progressBar.Maximum;
                newProgressBar.Value = progressBar.Value;
                
                Double max = progressBar.Maximum;
                Double val = progressBar.Value;
                int pDone = (int)(val / (max / 100));
                if (pDone >= 100)
                {
                    if (newProgressBar.Value == newProgressBar.Maximum)
                    {
                        this.Completed = true;
                        return 100;
                    }
                    return 99;
                }
                return pDone;
            }
        }

        public ProgressBar ProgressBar
        {
            get
            {

                if (newProgressBar == null || newProgressBar.IsDisposed)
                {
                    newProgressBar = new ProgressBar();
                    newProgressBar.Maximum = progressBar.Maximum;
                    newProgressBar.Value = progressBar.Value;
                }
                return newProgressBar;
            }
            set
            {
                progressBar = value;
                if (newProgressBar == null || newProgressBar.IsDisposed)
                {
                    newProgressBar = new ProgressBar();
                    newProgressBar.Maximum = progressBar.Maximum;
                    newProgressBar.Value = progressBar.Value;
                }
            }
        }

        private bool showOnDone;
        public bool ShowOnDone
        {
            get
            {
                return showOnDone;
            }
            set
            {
                showOnDone = value;
            }
        }

        private bool completed;
        public bool Completed
        {
            get
            {
                return completed;
            }
            set
            {
                completed = value;
            }
        }

        private Control control;
        public Control ParentControl
        {
            get
            {
                return control;
            }
            set
            {
                control = value;
            }
        }


    }
}
